﻿#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 40
#define cla 4
void Readscore(struct STUDENT stu[], int n);
void Addstu(struct STUDENT stu[], int n);
int Aver(struct STUDENT stu[], int n);
void Addcourse(struct STUDENT stu[], int m);
void Sort(struct STUDENT stu[], int m, int(*compare)(int a, int b),int n);
int descendingsort(int a,int b);
int Ascendingsort(int a, int b);
int Idsort(int a, int b);
void printscore(struct STUDENT stu[], int m);
void printOnescore(struct STUDENT stu[], int m);
void Namesort(struct STUDENT stu[], int m);
void Namesearch(struct STUDENT stu[], int m);
void IDsearch(struct STUDENT stu[], int m);
void All(struct STUDENT stu[], int m);
void Writetofile(struct STUDENT stu[], int m,int n);
void Readfromfile(struct STUDENT stu[], int *m, int n);//与书上有点不同
struct Date
{
	int year;
	int month;
	int day;
};
struct STUDENT
{
	long ID;
	char Name[10];
	char Sex;
	struct Date birthday;
	int score[cla];
	int addscore[2];
};
int main()
{
	int m;//人数
	int n;//执行符号
	struct STUDENT stu[N];
	printf("1.输入学号,姓名，性别，生日和四科成绩\n2.计算总分和平均分\n3.降序排列\n4.升序排列\n5.按照姓名排列\n6.按照id号排列\n");
	printf("7.打印课程的总分和平均分\n8.0打印所有成绩\n9.按ID搜索成绩\n10.按姓名搜索\n11.统计分数\n12.把数据写入文件\n0.退出\n");
	do
	{
		printf("\n");
		printf("n=");
		scanf("%d", &n);
		if (n == 1)
		{
			printf("有多少学生？");
			scanf("%d", &m);
			Readscore(stu, m);
		}
		else if (n == 2)
		{
			printf("计算总分和平均分\n");
			Addstu(stu, m);
		}
		else if (n == 3)
		{
			printf("成绩降序排列\n");
			Sort(stu, m,Ascendingsort,n);
			printscore(stu, m);
		}
		else if (n == 4)
		{
			printf("成绩升序排列\n");
			Sort(stu, m,descendingsort,n);
			printscore(stu, m);
		}
		else if (n == 5)
		{
			printf("按照姓名排列\n");
			Namesort(stu, m);
			printscore(stu, m);
		}
		else if (n == 6)
		{
			printf("按照id号排列\n");
			Sort(stu, m, Idsort, n);
			printscore(stu, m);
		}
		else if (n==7)
		{
			printf("打印课程的总分和平均分\n");
			Addcourse(stu, m);
		}
		else if (n==8)
		{
			printf("打印所有成绩\n");
			printscore(stu, m);
		}
		else if (n==9)
		{
			printf("按ID搜索成绩\n");
			IDsearch(stu, m);
		}
		else if (n==10)
		{
			printf("按姓名搜索\n");
			Namesearch(stu, m);
		}
		else if (n == 11)
		{
			printf("记录每个分段占人数百分比\n");
			All(stu,m);
		}
		else if (n==12)
		{
			printf("把数据写入文件\n");
			Writetofile(stu, m, cla);
		}
		else if (n == 13)
		{
			printf(" \n");
			Readfromfile(stu, &m, cla);
		}
	} while (n != 0);
	fflush(stdin);
	system("pause");
	return 0;
}
void Readscore(struct STUDENT stu[], int m)
{
	int i, j;
	printf("\t学号  姓名 性别 生日\t四科成绩\n");
	for (i = 0; i < m; i++)
	{
		printf("学生%d：", i + 1);
		scanf("%ld", &stu[i].ID);
		scanf("%s", &stu[i].Name);
		scanf(" %c", &stu[i].Sex);
		scanf(" %c", &stu[i].Sex);
		scanf("%ld", &stu[i].birthday.year);
		scanf("%ld", &stu[i].birthday.month);
		scanf("%ld", &stu[i].birthday.day);
		for (j = 0; j < cla; j++)
		{
			scanf("%d", &stu[i].score[j]);
		}
		fflush(stdin);
	}
}
void Addstu(struct STUDENT stu[], int m)
{
	int i;
	for (i = 0; i < m; i++)
	{
		//for cla
		stu[i].addscore[0] = stu[i].score[1] + stu[i].score[2] + stu[i].score[3] + stu[i].score[0];
		printf("学生%d的总分=%3d    ", i + 1, stu[i].addscore[0]);
		stu[i].addscore[1] = stu[i].score[4] / 4;
		printf("学生%d的平均分=%3d  \n", i + 1, stu[i].addscore[1]);
	}
	printf("\n");
}
void Sort(struct STUDENT stu[], int m,int(*compare)(int a,int b),int n)//升序
{
	int i, j, k;
	for (i = 0; i < m; i++)
	{
		j = i;
		stu[j] = stu[i];
		for (k = j + 1; k < m; k++)
		{
			if (n == 3 || n == 4)
			{
				if ((*compare)(stu[i].addscore[0], stu[k].score[0]))
				{
					stu[m + 1] = stu[k];
					stu[k] = stu[j];
					stu[j] = stu[m + 1];
				}
			}
			else if (n==6)
			{
				if ((*compare)(stu[i].ID, stu[k].ID))
				{
					stu[m + 1] = stu[k];
					stu[k] = stu[j];
					stu[j] = stu[m + 1];
				}
			}
		}
	}
}
int Ascendingsort(int a, int b)//升序
{
	return a > b;
}
int descendingsort(int a,int b)//降序
{
	return a < b;
}
int Idsort(int a, int b)//id升序
{
	return a > b;
}
/*int Namesort(char a[], char b[])//id升序
{
	return	strcmp(a, b);
}*/
void Namesort(struct STUDENT stu[], int m)
{
	int i, j, k;
	for (i = 0; i < m; i++)
	{
		j = i;
		stu[j] = stu[i];
		for (k = j + 1; k < m; k++)
		{
			if (strcmp(stu[i].Name, stu[k].Name)>=0)
			{
				stu[m + 1] = stu[k];
				stu[k] = stu[j];
				stu[j] = stu[m + 1];
			}
		}
	}
}
void printscore(struct STUDENT stu[], int m)//打印很多只学生
{
	int i, j;
	for (i = 0; i < m; i++)
	{
		printf("学生%d ID：%6ld\t", i + 1, stu[i].ID);
		printf("姓名：%5s\t", stu[i].Name);
		printf("性别:%c\t", stu[i].Sex);
		printf("生日：%4d/%02d/%02d\t", stu[i].birthday.year, stu[i].birthday.month, stu[i].birthday.day);
		printf("四科成绩 总成绩 平均成绩 分别为：");
		for (int j = 0; j < cla; j++)//调试改动过
		{
			printf("%4d", stu[i].score[j]);
		}
		printf("%4d", stu[i].addscore[0]);
		printf("%4d", stu[i].addscore[1]);
		printf("\n");
	}
}
void Addcourse(struct STUDENT stu[], int n)
{
	int i, j;
	int course[cla];//反应课程总成绩
	int avercourse[cla];//平均成绩
	for (i = 0; i < cla; i++)
	{
		course[i] = 0;
		for (j = 0; j < n; j++)
		{
			course[i] = course[i] + stu[j].score[i];
		}
		printf("学科%d的总分是：%d   ", i + 1, course[i]);
	}
	printf("\n");
	for (i = 0; i < cla; i++)
	{
		avercourse[i] = course[i] / n;
		printf("学科%d的平均分是：%d  ", i + 1, avercourse[i]);
	}
	printf("\n");
}
void printOnescore(struct STUDENT stu[],int i)
{
	printf("学生%d ID：%6ld\t", i + 1, stu[i].ID);
	printf("姓名：%5s\t", stu[i].Name);
	printf("性别:%c\t", stu[i].Sex);
	printf("生日：%4d/%02d/%02d\t", stu[i].birthday.year, stu[i].birthday.month, stu[i].birthday.day);
	printf("四科成绩 总成绩 平均成绩 分别为：");
	for (int j = 0; j < cla; j++)//调试改动过
	{
		printf("%4d", stu[i].score[j]);
	}
	printf("%4d", stu[i].addscore[0]);
	printf("%4d", stu[i].addscore[1]);
	printf("\n");
}
void IDsearch(struct STUDENT stu[],int m)
{
	int id, x;
	printf("输入学生学号：");
	scanf("%ld", &id);
	for (int i = 0; i < m; i++)
	{
		if (id==stu[i].ID)
		{
			x = i;
		}
	}
	printOnescore(stu, x);
}
void Namesearch(struct STUDENT stu[],int m)
{
	int x;
	char name[10];
	printf("请输入姓名：");
	scanf("%s", name);
	for (int i = 0; i < m; i++)
	{
		if (strcmp(stu[i].Name,name)==0)
		{
			x = i;
		}
	}
	printOnescore(stu, x);
}
void All(struct STUDENT stu[],int m)
{
	int i, j;
	float a=0, b=0, c=0, d=0, e=0;
	for ( j = 0; j < cla; j++){
		printf("课程%d:", j);
		a = 0, b = 0, c = 0, d = 0, e = 0;
		for ( i = 0; i < m; i++){
			if (stu[i].score[j]>=90&&stu[i].score[j]<=100){
				a = a + 1;
			}
			else if (stu[i].score[j] >= 80 && stu[i].score[j] < 90){
				b = b + 1;
			}
			else if (stu[i].score[j] >= 70 && stu[i].score[j] < 80){
				c = c + 1;
			}
			else if (stu[i].score[j] >= 60 && stu[i].score[j] < 70){
				d = d + 1;
			}
			else if (stu[i].score[j] >= 00 && stu[i].score[j] < 60){
				e = e + 1;
			}
		}
		printf("90-100:%2.2f%%,80-90:%2.2f%%,70-80:%2.2f%%,60-70:%2.2f%%,60以下:%2.2f%%\n", (a * 100) / m, (b * 100) / m, (c * 100) / m, (d * 100) / m, (e * 100) / m);
	}
}
void Writetofile(struct STUDENT stu[], int m,int n)
{
	FILE*fp;
	int i, j;
	if ((fp = fopen("score.txt", "w")) == NULL)
	{
		printf("打开失败");
		exit(0);
	}
	fprintf(fp, "%d\t%d\n", m, 4);
	for ( i = 0; i < m; i++)
	{
		fprintf(fp, "%10ld%8s%3c%6d/%02d/%02d",
			stu[i].ID,
			stu[i].Name,
			stu[i].Sex,
			stu[i].birthday.year,
			stu[i].birthday.month,
			stu[i].birthday.day
			);
		for ( j = 0; j < n; j++)
		{
			fprintf(fp, "%4d",stu[i].score[j]);
		}
		fprintf(fp, "%6.1f", stu[i].score[6]);
	}
	fclose(fp);
}
void Readfromfile(struct STUDENT stu[], int *m, int n)
{
	FILE*fp;
	int i, j;
	if ((fp = fopen("score.txt", "w")) == NULL)
	{
		printf("打开失败");
		exit(0);
	}
	fscanf(fp, "%d\t%d\n", m, 4);
	for (i = 0; i < *m; i++)
	{
		fscanf(fp, "%10ld", &stu[i].ID);
		fscanf(fp, "%8s", &stu[i].Name);
		fscanf(fp, "% c", &stu[i].Sex);
		fscanf(fp, "%6d/%2d/%2d", &stu[i].birthday.year,
			                      &stu[i].birthday.month,
								  &stu[i].birthday.day);
		for (j = 0; j < n; j++)
		{
			fscanf(fp, "%4d", &stu[i].score[j]);
		}
		fscanf(fp, "%6.1f", &stu[i].score[6]);
	}
	fclose(fp);
}
/*
int low = 0, high = m - 1, mid,x;//折半搜索
	if (n == 5)
	{
		while (low <= high)
		{
			mid = (low + high) / 2;
			if (id > stu[mid].ID)
			{
				low = mid + 1;
			}
			else if (id < stu[mid].ID)
			{
				high = mid - 1;
			}
			else
			{
				x = mid;
			}
		}
	}
*/
